﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Text.Encodings.Web;

namespace RuoYi.Framework.Authentication.OAuth.QQ
{
    public class QQAuthenticationHandler : OauthAuthenticationHandler<QQAuthenticationOptions>
    {
        public QQAuthenticationHandler(IOptionsMonitor<QQAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, IHttpClientFactory httpClientFactory) : base(options, logger, encoder, httpClientFactory)
        {
        }

        public override string AuthenticationSchemeNmae => QQAuthenticationDefaults.AuthenticationScheme;

        protected override async Task<List<Claim>> GetAuthTicketAsync(string code)
        {
            //获取 accessToken
            var tokenQueryKv = new List<KeyValuePair<string, string?>>()
            {
                new KeyValuePair<string, string?>("grant_type","authorization_code"),
                new KeyValuePair<string, string?>("client_id",Options.ClientId),
                new KeyValuePair<string, string?>("client_secret",Options.ClientSecret),
                new KeyValuePair<string, string?>("redirect_uri",Options.RedirectUri),
                new KeyValuePair<string, string?>("fmt","json"),
                new KeyValuePair<string, string?>("need_openid","1"),
                new KeyValuePair<string, string?>("code",code)
            };
            var tokenModel = await SendHttpRequestAsync<QQAuthticationcationTokenResponse>(QQAuthenticationDefaults.TokenEndpoint, tokenQueryKv);

            //获取 userInfo
            var userInfoQueryKv = new List<KeyValuePair<string, string?>>()
            {
                new KeyValuePair<string, string?>("access_token",tokenModel.access_token),
                new KeyValuePair<string, string?>("oauth_consumer_key",Options.ClientId),
                new KeyValuePair<string, string?>("openid",tokenModel.openid),
            };
            var userInfoMdoel = await SendHttpRequestAsync<QQAuthticationcationUserInfoResponse>(QQAuthenticationDefaults.UserInformationEndpoint, userInfoQueryKv);

            List<Claim> claims = new List<Claim>()
            {
                new Claim(QQAuthenticationConstants.Claims.AvatarFullUrl, userInfoMdoel.figureurl_qq_2),
                new Claim(QQAuthenticationConstants.Claims.AvatarUrl, userInfoMdoel.figureurl_qq_1),
                new Claim(QQAuthenticationConstants.Claims.PictureFullUrl, userInfoMdoel.figureurl_2),
                new Claim(QQAuthenticationConstants.Claims.PictureMediumUrl, userInfoMdoel.figureurl_qq_1),
                new Claim(QQAuthenticationConstants.Claims.PictureUrl, userInfoMdoel.figureurl),

                new Claim(OAuthAuthenticationConstants.OpenId, tokenModel.openid),
                new Claim(OAuthAuthenticationConstants.Name, userInfoMdoel.nickname),
                new Claim(OAuthAuthenticationConstants.AccessToken, tokenModel.access_token),
            };
            return claims;
        }
    }
}
